<html>
<head>
<script type="text/javascript" src="../../assets/js/bootstrap-wysiwyg.js"></script>
	<link rel="stylesheet" href="../../assets/css/editor_style.css" type="text/css">
	<script src="https://mindmup.s3.amazonaws.com/lib/jquery.hotkeys.js"></script>
	<script type="text/javascript">
		$(function(){
			$("#editor").wysiwyg();
			$('#editor').cleanHtml();
		});
	</script>
	<style type="text/css">
		#editor {
			overflow:scroll; 
			max-height:150px;
		}
		.editor{
			position:relative;
			left:-180px;
			width:540px;
			}
			
	</style>
	<script type="text/javascript">
		$(function(){
			$("#update_news").live("hover",function(){
				var txt = $("#editor").html();
				$("#show").text(txt);
			});
		});
	</script>
	<script type="text/javascript">
		$(function(){
			$("#add_news_btn").live("click",function(){
				var txt = $("#editor").html();
				$("#show").text(txt);
			});
		});
	</script>
	<script type="text/javascript">
		$(function(){
			$(".btn_insert").live("click",function(){
				$("#imgbtn").click();
			});
		});
	</script>
	<script type="text/javascript">
		$(function(){
			$("#show_link").live("click",function(){
				$("#link").show();
			});
		});
	</script>
	<script type="text/javascript">	
		$(function(){
			$('html').click(function() {
				$("#link").hide();
			});
			$("#link").click(function(event) {
				  event.stopPropagation();
			});
		});
		</script>
</head>
<body>
<?php
session_start();
include("inc/config.inc");
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
						if(!$link) {
							die('Failed to connect to server: ' . mysql_error());
						}
						
						//Select database
						$db = mysql_select_db(DB_DATABASE);
						if(!$db) {
							die("Unable to select database");
						}
						
	// UPDATING NEWS - START
	if(isset($_POST['admin'])){
		$headers = "../admin/index.php?update=success";
		$headerf = "../admin/index.php?update=fail";
	}
	else {
		$headers = "../news.php?update=success";
		$headerf = "../news.php?update=fail";
	}
	if(isset($_POST['submit'])){
		$ntitle = $_POST['title'];
		$nbody = $_POST['body'];
		$category = $_POST['news_cat'];
		$nid = $_POST['nid'];
		$oldphoto = $_POST['oldphoto'];
		
		mysql_query ('SET NAMES utf8');
		mysql_query('SET CHARACTER SET utf8');
		
		$path = "../../uploads/";
		$actual_old_name = $path.$oldphoto;
		
		$valid_formats = array("jpg", "png", "gif", "bmp");
		//if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
		//{
		$name = $_FILES['photoimg']['name'];
		if($name!=''){
		$size = $_FILES['photoimg']['size'];
		$r=10;
		if(strlen($name))
		{
			$r =1;
			list($txt, $ext) = explode(".", $name);
			if(in_array($ext,$valid_formats))
			{
				$r =2;
				if($size<(1024*1024))
				{
					$r =3;
					$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
					$tmp = $_FILES['photoimg']['tmp_name'];
					
						//mysql_query("INSERT INTO users image value('$actual_image_name') ");
						$qry = mysql_query("UPDATE news SET title='$ntitle', content='$nbody', image='$actual_image_name' , category_id='$category', publish_date=NOW() WHERE id='$nid'");
												//echo "<img src='uploads/".$actual_image_name."'  class='preview'>";
					if($qry){
						move_uploaded_file($tmp, $path.$actual_image_name);
						unlink($actual_old_name);	
					}
				
					else
					{echo "failed";$r=77;}
				}
				else
				{echo "Image file size max 1 MB";$r=88;}
			}
			else
			{echo "Invalid file format..";$r=99;}
			
			if($qry){
				header("Location:".$headers);
			}
			else {
				header("Location:".$headerf);
			}
		}
		}
		else {
			$qry = mysql_query("UPDATE news SET title='$ntitle', content='$nbody', image='$oldphoto' , category_id='$category', publish_date=NOW() WHERE id='$nid'");
			if($qry){
				header("Location:".$headers);
			}
			else {
				header("Location:".$headerf);
			}	
		}
		
	}
	// UPDATING NEWS - FINISH
	

if(isset($_POST['post_id']))
{
$id=$_POST['post_id'];
$id = mysql_real_escape_String($id);
$sql = "select * from news where id='$id'";
$run=mysql_query( $sql);
$news_row=mysql_fetch_array($run);
?> 
<form class='form-horizontal' enctype="multipart/form-data" style="margin-left:-30px;" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<div class="control-group">
    <label class="control-label" for="inputTitle">Title:</label>
    <div class="controls">
    
      <input type="text" class="input-large" name="title" id="inputTitle" value='<?php echo $news_row['title'];?>'>
    </div>
  </div>
  
  <div class="control-group">
      <!-- E-mail -->
      <label class="control-label">Category</label>
      <div class="controls">
      			<div id="news_cat_new" name="news_cat" class="span12" style="display: none"></div>
      <?php
$cat_qry = mysql_query("SELECT * FROM news_categories order by name asc");
			if($cat_qry){
				?>
				<select name="news_cat" id="news_cat" class="input-large">
				<option value="">Please select a category...</option>
					<?php 
					while($cat_row=mysql_fetch_array($cat_qry)){
						if($cat_row['id']==$news_row['category_id']){
						?>
						<option value="<?php echo $cat_row['id']; ?>" selected><?php echo $cat_row['name']; ?></option>
						<?php
						}
						else {
							?>
						<option value="<?php echo $cat_row['id']; ?>"><?php echo $cat_row['name']; ?></option>
						<?php
						}
					}
					?>
				</select>
				<?php
			}	
			else{
				echo "QUERY FAILED: " . mysql_error();
			}
       						
			$select_user=mysql_query("SELECT * FROM perm_members where id='$news_row[publisher_id]'");
			$user_row=mysql_fetch_array($select_user);
			$user_name=$user_row['firstname'] . " " . $user_row['lastname'];
		?>
        <input type="hidden" name="nid" value="<?php echo $id; ?>" />
        
      </div>
    </div>
      <div class="control-group">
      <!-- News Body-->
      <label class="control-label" for="password">News Body: </label>
      <div class="controls">
      <?php if(!isset($_POST['admin'])){ ?>
        <div class="controls">
     <div id="alerts"></div>
    <div class="btn-toolbar editor" data-role="editor-toolbar" data-target="#editor">
     <div class="btn-group">
        <a class="btn dropdown-toggle" data-toggle="dropdown" title="Font Size"><i class="icon-text-height"></i>&nbsp;<b class="caret"></b></a>
          <ul class="dropdown-menu">
          <li><a data-edit="fontSize 5"><font size="5">Big</font></a></li>
          <li><a data-edit="fontSize 3"><font size="3">Medium</font></a></li>
          <li><a data-edit="fontSize 1"><font size="1">Small</font></a></li>
          </ul>
      </div>
      <div class="btn-group">
        <a class="btn" data-edit="bold" title="Bold (Ctrl/Cmd+B)"><i class="icon-bold"></i></a>
        <a class="btn" data-edit="italic" title="Italic (Ctrl/Cmd+I)"><i class="icon-italic"></i></a>
        <a class="btn" data-edit="underline" title="Underline (Ctrl/Cmd+U)"><i class="icon-underline"></i></a>
      </div>
     <div class="btn-group">
        <a class="btn" data-edit="justifyleft" title="Align Left (Ctrl/Cmd+L)"><i class="icon-align-left"></i></a>
        <a class="btn" data-edit="justifycenter" title="Center (Ctrl/Cmd+E)"><i class="icon-align-center"></i></a>
        <a class="btn" data-edit="justifyright" title="Align Right (Ctrl/Cmd+R)"><i class="icon-align-right"></i></a>
        <a class="btn" data-edit="justifyfull" title="Justify (Ctrl/Cmd+J)"><i class="icon-align-justify"></i></a>
      </div>
      <div class="btn-group">
        <a class="btn" data-edit="insertunorderedlist" title="Bullet list"><i class="icon-list-ul"></i></a>
        <a class="btn" data-edit="insertorderedlist" title="Number list"><i class="icon-list-ol"></i></a>
      </div>
     <div class="btn-group">
		  <a id="show_link" class="btn" title="Hyperlink"><i class="icon-link"></i></a>
		    <div class="dropdown-menu input-append" id="link" style="display:none;">
			    <input class="span9" placeholder="URL" type="text" data-edit="createLink"/>
			    <button class="btn" type="button">Add</button>
        	</div>
        <a class="btn" data-edit="unlink" title="Remove Hyperlink"><i class="icon-cut"></i></a>

      </div>
        <a class="btn btn_insert" title="Insert picture (or just drag & drop)" id="pictureBtn"><i class="icon-picture"></i></a>
        <input id="imgbtn" style="display:none;" type="file" data-role="magic-overlay" data-target="#pictureBtn" data-edit="insertImage" />
     </div>

    <div id="editor" class="editor"><?php echo $news_row['content']; ?></div>
    <textarea rows="7" id='show' style="display:none;" name='body' class='span11' required></textarea>
      </div>
      <?php }
      else {
	 ?>
	    <textarea rows="7" id='news_body' name='body' class='span5'><?php echo $news_row['content']; ?></textarea>
	    <input type="hidden" name="admin" value="yes"/>
	 <?php 
	 } 
	 ?>
      </div>
	  </div>
	  
	  <div class="control-group">
      <label class="control-label">&nbsp;</label>
      <div class="controls">	
      					<?php $photo = $news_row['image']; 
		             		if(!isset($_POST['admin'])){ ?>
		            		<img src="../uploads/<?php echo $photo; ?>" class='span3 img-polaroid' style="height: 180px; margin-left:0;">
      					<?php }
     					 else {
	 					?>
		    		        <img src="../../uploads/<?php echo $photo; ?>" class='span3 img-polaroid' style="height: 180px; margin-left:0;">
	 	 				<?php 
	 					} 
						?>
						<input type="hidden" name="oldphoto" value="<?php echo $photo; ?>"/>
		</br>
		
		</div>
		
		</div>
		
		
	  <div class="control-group">
      <label class="control-label">Change image: </label>
      <div class="controls">	
      
		<input type="file" name="photoimg" id="photoimg" />
		
		</br>
		
		</div>
		
		</div>
	  <div class="control-group">
	  <label class="control-label">&nbsp;</label>
	  <div class="controls">
	  	<input type="submit" name="submit" id="update_news" class="btn btn-success span2" value="Update"/>
      </div>	
      </div>
  
</form>
</body>
</html>
<?php 
}
?>